package com.pan.crawler.impl6;

import com.pan.crawler.Directory;
import com.pan.entity.BookDirectory;
import com.pan.entity.Chapter;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

import java.util.List;
import java.util.stream.Collectors;

public class DirectoryImpl6 implements Directory {
//     * 这个网站貌似最低间隔是2.5秒，低于2.5秒就会被网关拦截,封半个小时ip
    private static final String url = "www.121ds.cc";
    @Override
    public BookDirectory get(String url) {
        if (-1 == url.indexOf(DirectoryImpl6.url)){
            throw new RuntimeException("传入网址错误："+url);
        }
        EdgeDriver edgeDriver = new EdgeDriver();
        edgeDriver.navigate().to(url);
        WebElement dlElement = edgeDriver.findElement(By.className("clearfix"));
        List<WebElement> aList = dlElement.findElements(By.cssSelector("a"));
        int[] i = new int[]{0};
        List<Chapter> chaptetList = aList.stream().map(aE -> {
            String href = aE.getAttribute("href");
            String title = aE.getText();
            return new Chapter(i[0]++, title, href);
        }).collect(Collectors.toList());
        String bookName = edgeDriver.findElement(By.className("book-title")).getText();
        edgeDriver.quit();
        return new BookDirectory().setChapterList(chaptetList).setBookName(bookName);
    }
}
